//
//  ViewController.m
//  UI_06作业滚动视图
//
//  Created by dllo on 16/6/13.
//  Copyright © 2016年 dllo. All rights reserved.
//
#define ScreeenWidth [UIScreen mainScreen].bounds.size.width
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
#import "ViewController.h"
#import "BigScrollView.h"
@interface ViewController ()<UIScrollViewDelegate>
@property (nonatomic,retain)UIScrollView *scrollView;
@property (nonatomic,retain)UIPageControl *pageControl;
@property (nonatomic,assign)BOOL isBing;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createScrollView];
    
}
- (void)createScrollView{
    UIImage *image1 = [UIImage imageNamed:@"1.JPG"];
    UIImage *image2 = [UIImage imageNamed:@"2.JPG"];
    UIImage *image3 = [UIImage imageNamed:@"3.JPG"];
    UIImage *image4 = [UIImage imageNamed:@"4.JPG"];
    UIImage *image5 = [UIImage imageNamed:@"5.JPG"];
    UIImage *image6 = [UIImage imageNamed:@"6.JPG"];
    self.photoArray = @[image1,image2,image3,image4,image5,image6];
    //创建大滚动视图
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreeenWidth, ScreenHeight)];
    self.scrollView.delegate = self;
    //容量
    self.scrollView.contentSize = CGSizeMake(ScreeenWidth * (self.photoArray.count + 2), ScreenHeight);
    self.scrollView.contentOffset = CGPointMake(ScreeenWidth, 0);
    //是否整页翻动
    
    self.scrollView.pagingEnabled = YES;
    [self.view addSubview:_scrollView];
    //创建小滚动视图
    for (NSInteger i = 1; i <= self.photoArray.count; i++) {
        UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(ScreeenWidth * i, 0, ScreeenWidth, ScreenHeight)];
        scroll.delegate = self;

        scroll.minimumZoomScale = 0.5;
        scroll.maximumZoomScale = 2;
        scroll.zoomScale = 1;
        [self.scrollView addSubview:scroll];
        self.imageView =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreeenWidth, ScreenHeight)];
        self.imageView.image = [self.photoArray objectAtIndex:i - 1];
        self.imageView.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapImageView:)];
        tap.numberOfTapsRequired = 2;
        [self.imageView addGestureRecognizer:tap];
        [scroll addSubview:_imageView];

    }
    //在最前面添加最后一张
    UIScrollView *frontScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreeenWidth, ScreenHeight)];
    frontScrollView.contentSize = CGSizeMake(ScreeenWidth, ScreenHeight);
    [self.scrollView addSubview:frontScrollView];
    [frontScrollView release];
    UIImageView *frontImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreeenWidth, ScreenHeight )];
    frontImageView.image = [self.photoArray lastObject];
    [frontScrollView addSubview:frontImageView];
    [frontImageView release];
   //  最后面添加第一张
    UIScrollView *backScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(ScreeenWidth * (self.photoArray.count + 1), 0, ScreeenWidth, ScreenHeight)];
    backScrollView.contentSize = CGSizeMake(ScreeenWidth, ScreenHeight);
    [self.scrollView addSubview:backScrollView];
    [backScrollView release];
    UIImageView *backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreeenWidth, ScreenHeight)];
    backImageView.image = [self.photoArray firstObject];
    [backScrollView addSubview:backImageView];
    [backImageView release];

    
    //初始化分页控件:
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(60, 500, 255, 30)];
    self.pageControl.numberOfPages = self.photoArray.count;

    [self.pageControl addTarget:self action:@selector(didClickedPageControl:) forControlEvents:UIControlEventValueChanged];
    
    [self.view addSubview:_pageControl];
    [_pageControl release];

    
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return scrollView.subviews.firstObject;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    for (UIScrollView *scroll in scrollView.subviews) {
//        isKindOfClass来确定一个对象是否是一个类的成员，或者是派生自该类的成员
//        　　isMemberOfClass只能确定一个对象是否是当前类的成员
        if ([scroll isKindOfClass:[UIScrollView class]]) {
            scroll.zoomScale = 1.0;
        }
    }

    if (scrollView.contentOffset.x == 0) {
        self.scrollView.contentOffset = CGPointMake(ScreeenWidth * self.photoArray.count, 0);
    }
    if (scrollView.contentOffset.x == (self.photoArray.count + 1) * ScreeenWidth) {
        self.scrollView.contentOffset = CGPointMake(ScreeenWidth, 0);
    }
    self.pageControl.currentPage = self.scrollView.contentOffset.x / ScreeenWidth - 1;
}
- (void)didTapImageView:(UITapGestureRecognizer *)tap
{
    //双击放大:
    if (_isBing == NO) {
        [(UIScrollView *)tap.view.superview zoomToRect:[self zoomRectByScale:3.0 Center:[tap locationInView:tap.view]] animated:YES];
      
        _isBing = YES;
    }
    else
    {
        [(UIScrollView *)tap.view.superview setZoomScale:1.0 animated:YES];
        _isBing = NO;
    }
}

- (CGRect)zoomRectByScale:(CGFloat)scale Center:(CGPoint)center
{
    CGRect zoomRect;
    zoomRect.size.width = ScreeenWidth / scale;
    zoomRect.size.height = ScreenHeight / scale;
    //找到新的原点:
    zoomRect.origin.x = center.x - center.x / scale;
    zoomRect.origin.y = center.y - center.y / scale;
    return zoomRect;
}
- (void)didClickedPageControl:(UIPageControl *)pageControl
{
    [self.scrollView setContentOffset:CGPointMake((pageControl.currentPage + 1) * ScreeenWidth, 0) animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
